<template>
  <div class="login-container">
    <el-form ref="form" :model="form" label-width="80px" class="login-form" :rules="rules">
      <h2 class="login-title">梦学谷会员管理系统</h2>
      <el-form-item label="账号" prop="username">
        <el-input v-model="form.username"></el-input>
      </el-form-item>
      <el-form-item label="密码" prop="password">
        <el-input v-model="form.password" type="password"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm('form')">登录</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import {login, getUserInfo} from '@/api/login'

export default {
  data() {
    return {
      form: {
        username: '',
        password: ''
      },
      // 账号密码验证规则
      rules: {
        username: [
          {required: true, message: '请输入账号', trigger: 'blur'}
        ],
        password: [
          {required: true, message: '请输入密码', trigger: 'blur'}
        ]
      }
    };
  },
  
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate(valid => {
        if(valid){
          // 提交表单验证
          this.$store.dispatch('Login', this.form).then(response => {
            if(response.flag){
              this.$router.push('/')
            }else{
              this.$message({
                message: response.message,
                type: 'warning'
              })
            }
          }).catch(error => {

          })
        }else{
          this.$message({
                message: '验证失败',
                type: 'warning'
              });
        }
      })
    }
  }
};
</script>

<style scoped>
    .login-form{
        width: 350px;
        margin: 160px auto;
        background-color: rgb(255, 255, 255, 0.8);
        padding: 30px;
        border-radius: 20px;
    }
    .login-title{
        text-align: center;
        color: #303133;
    }
    .login-container{
        position: absolute;
        width: 100%;
        height: 100%;
        background: url('../../assets/login.png');
        background-repeat: no-repeat;
        background-size: cover;
    }
</style>